package service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import model.Action;
import model.History;
import model.SaleData;
import tools.Util;

public class Stuff {
	public static Map<String,Action> allactions = new HashMap<String,Action>();
	
	static {
		//TODO: persistant:load from a file or database
		allactions.put("1",new Action("1","sys","","[@target]被[@viewer]释放了."));
		allactions.put("2",new Action("2","sys","","[@viewer]为自己赎了身,从此又是自由人了."));
		allactions.put("3",new Action("3","sys","","[@target]被[@viewer]放到跳蚤市场,打折处理."));
		
		
		allactions.put("11",new Action("11","bad","痛扁一顿","[@target]被[@viewer]痛扁了一顿.[@target]痛得直叫娘."));
		allactions.put("12",new Action("12","bad","饿一天不给饭吃","[@viewer]一整天都不给[@target]饭吃,[@target]饿得双眼放绿光."));
		allactions.put("13",new Action("13","bad","关小黑屋一天","[@viewer]把[@target]关进了小黑屋,[@target]害怕得哭了."));
		allactions.put("14",new Action("14","bad","送去卖唱","[@target]被[@viewer]派去歌厅卖唱,[@target]哭喊着\"小人卖身不卖艺啊\""));
		
		allactions.put("31",new Action("31","good","给他泡杯茶","[@viewer]给[@target]泡了一杯茶,亲自送到[@target]面前."));
		allactions.put("32",new Action("32","good","带他泡温泉","[@viewer]带着[@target]到了郊区,在一片白雪包围的温泉里,两个人泡的不亦乐乎斯基."));
		allactions.put("33",new Action("33","good","请他喝酒","[@viewer]请[@target]喝酒,不出一会,两人就干下去两箱茅台."));
		allactions.put("34",new Action("34","good","给他做马杀鸡","[@viewer]对[@target]说:\"平时打工辛苦你了\",说着就开始给[@target]做起了马杀鸡,[@target]感动得热泪盈眶."));
		
		allactions.put("51",new Action("51","flirt","给他请安","[@viewer]半跪在地上对[@target]说:\"给主子请安!\"."));
		allactions.put("52",new Action("52","flirt","称赞主人","[@viewer]大声称颂道：主人[@target]才高八斗、学富五车，英明神武、一统江湖！"));
	}
	
	public static String getHistory(String stuffid){
		return allactions.get(stuffid).getHistory();
	}
	
	/**
	 * 具体执行动作
	 * @param viewer 注意:这里是viewer
	 * @param slave 传过来的slave
	 * @param stuffid 动作id
	 * @return
	 */
	public static String execute(SaleData viewer,SaleData target,String stuffid){
		String result = "1";
		int sid = Integer.parseInt(stuffid);
		boolean defaultHistory = true;
		
		switch(sid){
		case 1://释放奴隶
			//check master and slave relation
			if(!FriendSale.isSlave(viewer,target))
				return target.getPerson().getName()+"不是你的奴隶!";
			
			String slaveid = target.getId();
			viewer.getSlaves().remove(slaveid);
			target.setMaster(null);
			target.setNickname("");
			
			target.setStatus(null);
			target.setBuyUntil(new Date());
			break;
		case 2://赎身
			if(viewer.getCash() < viewer.getPrice()){
				result = "你的现金不够,无法赎身.";
				return result;
			}
			
			SaleData master = FriendSale.getDataObj(viewer.getMaster().getId());
			//TODO:make profit
			master.getSlaves().remove(viewer.getId());
			viewer.setMaster(null);
			viewer.setNickname("");
			viewer.setCash(viewer.getCash() - viewer.getPrice());
			master.setCash(master.getCash() + viewer.getPrice());
			
			viewer.setStatus(null);
			viewer.setBuyUntil(new Date());
			
			target = master;
			defaultHistory = false;
			History history = generateHistory(viewer,target,stuffid);
			result = history.displayText(Util.convertToGlobal(viewer.getDomain(),viewer.getId()));
			
			generateCustomHistory("["+target.getId()+"]收回了¥"+viewer.getPrice()+".",target);
			break;
		case 3://打折
			break;
		case 13://关小黑屋一天
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE, 1);
			target.setBuyUntil(c.getTime());
			target.setStatus("被关小黑屋中...");
		default:

		}
		
		if(defaultHistory){
			History history = generateHistory(viewer,target,stuffid);
			result = history.displayText(Util.convertToGlobal(viewer.getDomain(),viewer.getId()));
		}
		
		return result;
	}
	
	public static History generateHistory(String viewer,String target,String stuffid) {
		SaleData viewerdata = FriendSale.getDataObj(viewer);
		SaleData targetdata = FriendSale.getDataObj(target);
		return generateHistory(viewerdata,targetdata,stuffid);
	}
	
	/**
	 * 取出stuffid 对应的消息,将消息中的参数替换成实际的用户.
	 * @param viewer
	 * @param target
	 * @param stuffid
	 * @return
	 */
	public static History generateHistory(SaleData viewer, SaleData target,String stuffid) {
		String message = Stuff.getHistory(stuffid);
		
		if(target.getMaster() != null)
		message = message.replaceAll("@target.master", target.getMaster().getId());
		
		message = message.replaceAll("@target.price", target.getPrice()+"");
		message = message.replaceAll("@viewer", viewer.getId());
		message = message.replaceAll("@target", target.getId());

//		History h = new History(message,new Date());
//		viewer.getHistory().add(h);
//		target.getHistory().add(h);
//		
//		return h;
		
		return generateCustomHistory(message,viewer,target);
	}
	
	public static History generateCustomHistory(String message,SaleData...ids){
		History h = new History(message,new Date());
		for(SaleData s:ids){
			s.getHistory().add(h);
		}
//		viewer.getHistory().add(h);
//		target.getHistory().add(h);
		return h;
	}
	
	/**
	 * 需要显示到页面的选项
	 * @param type
	 * @return
	 */
	public static Map<String,String> getDisplay(String type){
		Map<String,String> ret = new HashMap<String,String>();
			for(Entry<String,Action> action:allactions.entrySet()){
				if(action.getValue().getType().equals(type)){
					ret.put(action.getKey(), action.getValue().getOption());
				}
			}
		return ret;
	}
}
